package com.tcm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcm.common.enums.CachePrefix;
import com.tcm.entity.CommentLike;
import com.tcm.entity.bo.UserCacheBo;
import com.tcm.service.CommentLikeService;
import com.tcm.mapper.CommentLikeMapper;
import com.tcm.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Guqier
 * @description 针对表【ts_comment_like(评论点赞表)】的数据库操作Service实现
 * @createDate 2023-09-09 21:18:17
 */
@Service
public class CommentLikeServiceImpl extends ServiceImpl<CommentLikeMapper, CommentLike>
        implements CommentLikeService {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 判断用户是否点赞
     * @param commentId 评论ID
     * @param account   用户账号
     * @return 是否点赞
     */
    @Override
    public boolean isLike(Long commentId, String account) {
        int count = this.count(new LambdaQueryWrapper<CommentLike>().eq(CommentLike::getCommentId, commentId).eq(CommentLike::getAccount, account));
        return count != 0;
    }

    /**
     * 获取点赞数
     * @param commentId 评论ID
     * @return 评论点赞数
     */
    @Override
    public Integer getLikeNum(Long commentId){
        return this.count(new LambdaQueryWrapper<CommentLike>().eq(CommentLike::getCommentId, commentId));
    }

    /**
     * 添加评论点赞
     * @param commentId  评论ID
     * @param currUserId 当前登录用户ID
     * @return 添加结果
     */
    @Override
    public boolean addCommentLike(Long commentId, Long currUserId) {
        // 获取学生信息
        UserCacheBo userCacheBo = redisUtil.getCacheObject(CachePrefix.PC_USER_TOKEN.getCacheKey(currUserId.toString()));
        // 创建实体类
        CommentLike commentLike = new CommentLike();
        commentLike.setCommentId(commentId);
        commentLike.setAccount(userCacheBo.getAccount());
        commentLike.setCreateUser(currUserId);
        commentLike.setUpdateUser(currUserId);
        return this.save(commentLike);
    }

    /**
     * 删除评论点赞
     * @param commentId 评论ID
     * @param currUserId 当前用户ID
     * @return 处理结果
     */
    @Override
    public boolean removeCommentLike(Long commentId, Long currUserId){
        // 获取学生信息
        UserCacheBo userCacheBo = redisUtil.getCacheObject(CachePrefix.PC_USER_TOKEN.getCacheKey(currUserId.toString()));
        return this.remove(new LambdaQueryWrapper<CommentLike>().eq(CommentLike::getCommentId, commentId).eq(CommentLike::getAccount, userCacheBo.getAccount()));
    }

}




